$(document).ready(function() {
	let countdown = 120; // 2分钟倒计时
	let timer = null;

	// 发送验证码
	$('#sendCodeBtn').on('click', function() {
		const phone = $('#phone').val();
		if (!phone) {
			toastr.warning('请输入手机号');
			return;
		}

		// 这里添加发送验证码的逻辑
		const btn = $(this);
		btn.prop('disabled', true);
		const data = {
		        phone: phone
		    };
		//调用短信接口
		ajaxGet('login/getVerifyCode',data,'GET',function(response){
			if(response.code == 200){
				toastr.success('短信发送成功');
			} else{
				toastr.error(response.msg);
			}
		});
		timer = setInterval(function() {
			if (countdown > 0) {
				btn.text(countdown + '秒后重新发送');
				countdown--;
			} else {
				clearInterval(timer);
				btn.prop('disabled', false);
				btn.text('发送验证码');
				countdown = 120;
			}
		}, 1000);
	});

	// 注册表单提交
	$('#registerForm').on('submit', function(e) {
		e.preventDefault();
		const userName = $('#username').val();
		const password = $('#password').val();
		const confirmPassword = $('#confirmPassword').val();
		const phone = $('#phone').val();
		const code = $('#verifyCode').val();

		if (password !== confirmPassword) {
			toastr.warning('两次输入的密码不一致');
			return;
		}
		const data = {
			userName:userName,
			password:password,
			phone:phone,
			code:code
		};
		ajaxPost('login/register',data,'POST',function(response){
			if(response.code == 200){
				toastr.success('注册成功！');
				window.location.href = 'home.html';
			} else{
				toastr.error(response.msg);
			}
		});

		// if (username && password && phone && verifyCode) {
		// 	// 这里添加注册逻辑
		// 	localStorage.setItem('isLoggedIn', 'true');
		// 	window.location.href = 'login.html';
		// } else {
		// 	alert('请填写完整信息');
		// }
	});
});